package org.mm.ec.search.controller;

import java.util.ArrayList;
import java.util.List;

import org.mm.ec.search.service.SearchVediosService;
import org.mm.ec.search.store.Vedios;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 搜索控制
 * 
 */
@Controller
@RequestMapping("/")
public class SearchController {
	@Autowired
	private SearchVediosService searchVediosService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView home() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("home");
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/search")
	public ModelAndView search(@RequestParam("q") String query,
			@RequestParam("from") int from,
			@RequestParam("size") int size) {
		List<Vedios> list = searchVediosService.searchs(query, from,  size);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("search");
		mv.addObject("articles", list);
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/search/create")
	public ModelAndView createInitialData() {
		List<Vedios> listVedio = new ArrayList<Vedios>();
		for(int i = 1;i<100;i++){
			Vedios e = new Vedios();
			e.setId(i);
			e.setName("测试"+i);
			e.setTitle("仅仅是个测试而已"+i);
			e.setVedioThumb("http://91share.duapp.com");
			e.setDesc("我的一个elasticSearch-demo"+i);
			listVedio.add(e);
		}
		searchVediosService.createSearchIndex(listVedio);
		ModelAndView mv = new ModelAndView("forward:/");
		mv.addObject("message", "文章索引已创建成功!");
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/about")
	public ModelAndView about() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("about");
		return mv;
	}
}
